<?php
// 在图形上绘制文字
header('Content-type: image/png');
$res = imagecreatetruecolor(500, 500);
// 创建颜色
$white = imagecolorallocate($res, 255, 255, 255);
$red = imagecolorallocate($res, 255, 0, 0);
// 填充颜色
imagefill($res, 0, 0, $white);
$text = "hello world";
$font = realpath('MAIAN.TTF');
$box = imagettfbbox(20, 0, $font, $text);
$width = $box[2] - $box[0];
$height = $box[1] - $box[7];
// echo $width;
// echo $height;
// 居中显示
imagettftext($res, 22, 0, 250 - $width / 2, 250 - $height / 2, $red, $font, $text);

imagepng($res);
imagedestroy($res);